package com.nercel.dsj.gksales.api.controller;

import com.nercel.dsj.gksales.model.QueryParams;
import com.nercel.dsj.gksales.model.ResponseResult;
import com.nercel.dsj.gksales.model.entity.RegionPayCode;
import com.nercel.dsj.gksales.model.entity.User;
import com.nercel.dsj.gksales.service.RegionPayCodeService;
import com.nercel.dsj.gksales.service.UserService;
import com.nercel.dsj.gksales.util.ApiResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Api(tags="支付码")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PayCodeApiController extends BaseApiController {

    private static Logger logger = LoggerFactory.getLogger(PayCodeApiController.class);

    private final RegionPayCodeService regionPayCodeService;

    private final UserService userService;

    @ApiOperation(value = "支付码查询")
    @RequestMapping(value="/api/v1/paycode", method= RequestMethod.GET)
    public List<RegionPayCode> list(
            @ApiParam(hidden = true) QueryParams queryParams,
            @ApiParam(value = "页码") @RequestParam(required = false) Integer page,
            @ApiParam(value = "单页数量") @RequestParam(required = false) Integer pageSize,
            @ApiParam(value = "支付类型(微信1 支付宝2 沃3)") @RequestParam(required = false) Integer payType,
            @ApiParam(value = "区域ID") @RequestParam(required = false) Integer regionId,
            @ApiParam(value = "上传人ID") @RequestParam(required = false) Integer uploadUserId) {
        queryParams.put("payType", payType);
        queryParams.put("regionId", regionId);
        queryParams.put("uploadUserId", uploadUserId);
        return regionPayCodeService.list(queryParams);
    }

    @ApiOperation(value = "支付码显示 省 > 市")
    @RequestMapping(value="/api/v1/paycode/show", method= RequestMethod.GET)
    public List<RegionPayCode> listForPay(
            @ApiParam(hidden = true) QueryParams queryParams) {
        User user = userService.findById(getUserId());
        queryParams.put("regionId", user.getProvinceId());
        List<RegionPayCode> list = regionPayCodeService.list(queryParams);
        if(list.size() > 0){
            return list;
        }
        queryParams.put("regionId", user.getCityId());
        return regionPayCodeService.list(queryParams);
    }


    @PostMapping(value = "/api/v1/paycode")
    @ApiOperation(value = "创建码")
    public ResponseResult add(@RequestBody RegionPayCode params) {
        params.setCreateTime(new Date());
        regionPayCodeService.save(params);
        return ApiResultUtil.success();
    }

    @DeleteMapping(value = "/api/v1/paycode/regions/{id}")
    @ApiOperation(value = "清除码")
    public ResponseResult del(@ApiParam(value = "关键词") @PathVariable Integer id) {
        regionPayCodeService.deleteByRegionId(id);
        return ApiResultUtil.success();
    }


}
